/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmProjectCirculatedController
 * 项目传阅反馈
 *
 * 修改纪录
 * 2021-07-14 版本：1.0 chengengwei 创建。
 * @version 版本：1.0
 * @author 作者：chengengwei
 * 创建日期：2021-07-14
 */

package com.topscomm.mpm.controller;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.basic.util.SQLAdapter;
import com.topscomm.cbo.pojo.CboApprovalFlowEntity;
import com.topscomm.mpm.MpmSystemConst;
import com.topscomm.mpm.controller.auto.MpmProjectCirculatedControllerAuto;
import com.topscomm.mpm.pojo.MpmProjectCirculatedEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.DateUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst.TapOperationTypeEnum;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: chengengwei
 * @date: 2021-07-14
 * @description:项目传阅反馈Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/projectCirculated")
public class MpmProjectCirculatedController extends MpmProjectCirculatedControllerAuto implements IMpmProjectCirculatedController {

    /**
	 * @author: chengengwei
	 * @date: 2021-07-14
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" enabled=1 ");
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_docno")) {
			String value = paraMap.get("qry_docno").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docno like '%{0}%'", value);
		}
		if (paraMap.containsKey("qry_begindocdate")) {
			String value = paraMap.get("qry_begindocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate >= {0} ", SQLAdapter.toDateLong(value + " 00:00:00"));
		}
		if (paraMap.containsKey("qry_enddocdate")) {
			String value = paraMap.get("qry_enddocdate").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and docdate <= {0}", SQLAdapter.toDateLong(value + " 23:59:59"));
		}
		if (paraMap.containsKey("qry_deptname")) {
			String value = paraMap.get("qry_deptname").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (deptid in ( SELECT id from CboDept where fullname like '%{0}%'))", value);
		}
		if (paraMap.containsKey("qry_username")) {
			String value = paraMap.get("qry_username").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and (username like '%{0}%')", value);
		}
		if (paraMap.containsKey("qry_deptcode")) {
			String value = paraMap.get("qry_deptcode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and deptcode like '{0}%'", value);
		}
		if (paraMap.containsKey("qry_state")) {
			String value = paraMap.get("qry_state").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and state={0}", value);
		}
		if (paraMap.containsKey("qry_where")) {
			String value = paraMap.get("qry_where").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and {0}", value);
		}
//		if (this.getUserBean().getUser().getAdminlevel() < 1)// 管理员、超级管理员，不控制权限
//		{
//			String dataPession = service.getDataPermission(TapOperationTypeEnum.Select, "", this.getUserBean());
//			if (StringUtil.isEmpty(dataPession))// 默认权限只能看本人，部门负责人为自己的部门和下级部门的数据
//			{
//				String deptcodes_header = ConvertUtil.convertToString(this.getUserBean().getUser().getValue("deptcodes_header"));
//				if (StringUtil.isEmpty(deptcodes_header))
//					sql.appendSingle(" and (usercode = '{0}' or deptcode ='{1}' or deptcode = '{2}' )", this.getUserBean().getUser().getCode(), this.getUserBean().getDept().getCode(), this
//							.getUserBean().getWorkDept().getCode());
//				else
//					sql.appendSingle(" and (deptcode in ({0})  or usercode = '{1}' or deptcode ='{2}' or deptcode = '{3}' )", deptcodes_header, this.getUserBean().getUser().getCode(), this
//							.getUserBean().getDept().getCode(), this.getUserBean().getWorkDept().getCode());
//			} else// 使用数据权限
//			{
//				sql.appendSingle(" and {0} ", dataPession);
//			}
//		}
		return sql.toString();
	}

	/**
	 * @author: chengengwei
	 * @date: 2021-07-14
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 *
	 * @description: 提交审批前
	 * @param dataMap
	 * @param paraMap
	 * @throws Exception
	 * @author: chengengwei
	 * @date: 2021-07-14
	 * @modify:
	 */
	@Override
	protected void beforeSubmit(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
		super.beforeSubmit(dataMap, paraMap);
		StringBufferProxy mailtitle = new StringBufferProxy();
		mailtitle.appendSingle("{0}{1}于{2}提交项目传阅反馈申请,单据编号{3}! ", dataMap.get("dispdeptcode"),
                dataMap.get("username"), DateUtil.dateToString(new Date(), "yyyy-MM-dd"), dataMap.get("docno"));
		dataMap.put("maintitle", mailtitle.toString());
        List<Map<String, Object>> flowIdList = flowDatacodeCache.getApprovalFlowListMap("ApprovalFlow_" +
                        MpmSystemConst.MpmProjectCirculated, this.getUserBean(), CboApprovalFlowEntity.FieldSortcode);
        if (flowIdList.size() == 0) {
            throw new ServiceException("没有配置审批流");
        }
        dataMap.put(MpmProjectCirculatedEntity.FieldFlowid, ConvertUtil.convertToLong(flowIdList.get(0).get("id")));
	}

    /**
     * @description: 处理反馈人code
     * @author: chengengwei
     * @date: 2021-07-14
     * @modify:
     */
	@Override
    protected void beforeInsert(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
        String tempcodes = ConvertUtil.convertToString(paraMap.get(MpmProjectCirculatedEntity.FieldCirculatedcode));
        if (!StringUtil.isEmpty(tempcodes)) {
            String circulatedcode = "," + new ArrayList<>(Arrays.asList(tempcodes.split(","))).stream()
                    .collect(Collectors.joining(",")) + ",";
            paraMap.put(MpmProjectCirculatedEntity.FieldCirculatedcode, circulatedcode);
        }
    }

    /**
     * @description: 处理反馈人code
     * @author: chengengwei
     * @date: 2021-07-14
     * @modify:
     */
	@Override
    protected void beforeUpdate(Map<String, Object> dataMap, Map<String, Object> paraMap) throws Exception {
        String tempcodes = ConvertUtil.convertToString(paraMap.get(MpmProjectCirculatedEntity.FieldCirculatedcode));
        if (!StringUtil.isEmpty(tempcodes)) {
            String circulatedcode = "," + new ArrayList<>(Arrays.asList(tempcodes.split(","))).stream()
                    .collect(Collectors.joining(",")) + ",";
            paraMap.put(MpmProjectCirculatedEntity.FieldCirculatedcode, circulatedcode);
        }
    }

	/**
	 * @description: 保存退回传阅人code
	 * @author: chengengwei
	 * @date: 2021-07-14
	 * @modify:
	 */
	@PutMapping(value = "/saveReturnedcirculatedCode")
	public ResponseResult<Object> saveReturnedcirculatedCode(@RequestBody(required = false) Map<String, Object> paraMap) {
		this.service.saveReturnedcirculatedCode(paraMap);
		return ResponseResult.ok("保存成功");
	}

    /**
     * @author: chengengwei
     * @date: 2021-07-16
     * @description:查询项目经理退回理由
     * @modify:
     */
    @GetMapping(value = "/getManangerOpinion")
    public ResponseResult<Object> getManangerOpinion(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> dataMap = this.service.getManangerOpinion(paraMap);
        return ResponseResult.ok(dataMap);
    }

    /**
     * @author: chengengwei
     * @date: 2021-07-16
     * @description:查询传阅反馈id
     * @modify:
     */
    @GetMapping(value = "/queryCirculatedIdByProjectCode")
    public ResponseResult<Object> queryCirculatedIdByProjectCode(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> dataMap = this.service.queryCirculatedIdByProjectCode(paraMap);
        return ResponseResult.ok(dataMap);
    }
}

